//
//  RGMainPageViewModel.m
//  RedGrass
//
//  Created by wukong on 2019/1/10.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGMainPageViewModel.h"
#import "HttpRequest.h"
#import "RGMainPageRouter.h"
#import "XLMShareSdkTool.h"
#import "RGRemoteVedioDownLoadHelper.h"


#define pageSize 10

@interface RGMainPageViewModel()

@property (weak, nonatomic)   id<RGMainPageViewProtocol> protocol;

@end


@implementation RGMainPageViewModel

-(instancetype) initWithProtocol:(id<RGMainPageViewProtocol>)protocol{
    if(self = [super init]){
        self.protocol = protocol;
        self.pagaNum = 1;
        self.mainCommentPageNum = 1;
        self.total = 0;
        self.currentVedioId = 0;
        self.currentVedioIndex = 0;
    }
    return self;
}

//获取推荐视频列表
-(void) gainRecommendVideoListWithPageNumIsFirstLoad:(BOOL)isFirstLoad isLoadMore:(BOOL)isLoadMore{
    NSString *token = [RGUserInfoModel getUserInfo].token;
    NSDictionary *dic = @{@"pageNum":@(_pagaNum),
                          @"token":SafeStr(token)
                          };
    if(isFirstLoad){
        [self.protocol showLoading:@""];
    }
    AA_WEAK_MAKE(self) weakSelf = self;
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/recommendVideo"];
    [quest post:dic success:^(id data) {
        if(isFirstLoad){
                [weakSelf.protocol dismissLoading];
        }
        NSArray *originalArr = [data objectForKey:@"videos"];
        NSError *error;
        if(originalArr.count){
            NSMutableArray *tempArr = [RGRecommendVideoModel arrayOfModelsFromDictionaries:originalArr error:&error];
            if(tempArr.count){
                if(isFirstLoad || !isLoadMore){
                      weakSelf.videoList = tempArr;
                }else{
                    if(weakSelf.videoList == nil){
                        weakSelf.videoList = tempArr;
                    }else{
                        [weakSelf.videoList addObjectsFromArray:tempArr];
                    }
                }
              
            }
        }
         dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if([weakSelf.videoList count]){
                if(!isLoadMore){
                    if([weakSelf.protocol respondsToSelector:@selector(reloadTableViewWithVedioList:)]){
                        [weakSelf.protocol reloadTableViewWithVedioList:weakSelf.videoList];
                    }
                }else{
                    if([weakSelf.protocol respondsToSelector:@selector(reloadTableViewWhenHaveMoreVedioWithVedioList:)]){
                        [weakSelf.protocol reloadTableViewWhenHaveMoreVedioWithVedioList:weakSelf.videoList];
                    }
                }
               
            }else{
                if([weakSelf.protocol respondsToSelector:@selector(reloadTableViewWithVedioList:)]){
                    [weakSelf.protocol reloadTableViewWithVedioList:[NSMutableArray array]];
                }
            }
             if(!isFirstLoad){
                 if(isLoadMore){
                     if([weakSelf.protocol respondsToSelector:@selector(viewMjFooterEndLoading)]){
                         [weakSelf.protocol viewMjFooterEndLoading];
                     }
                    
                 }else{
                     if([weakSelf.protocol respondsToSelector:@selector(viewMjHeaderEndLoading)]){
                         [weakSelf.protocol viewMjHeaderEndLoading];
                     }
                     if([weakSelf.protocol respondsToSelector:@selector(playWithFirstIndexWhenLoadSuccess)]){
                         [weakSelf.protocol playWithFirstIndexWhenLoadSuccess];
                     }
                 }
             }else{
                 if([weakSelf.protocol respondsToSelector:@selector(playWithFirstIndexWhenLoadSuccess)]){
                     [weakSelf.protocol playWithFirstIndexWhenLoadSuccess];
                 }
             }
         
         });
        
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.protocol showTextMessageWithMessage:message];
        if(!isFirstLoad){
            if(isLoadMore){
                if([weakSelf.protocol respondsToSelector:@selector(viewMjFooterEndLoading)]){
                    [weakSelf.protocol viewMjFooterEndLoading];
                }
                
            }else{
                if([weakSelf.protocol respondsToSelector:@selector(viewMjHeaderEndLoading)]){
                    [weakSelf.protocol viewMjHeaderEndLoading];
                }
            }
        }
    }];
}


//关注，取消关注
-(void) changeFocusStatusWithChosedIndex:(NSInteger)chosedIndex{
    if([_videoList count] == 0 || chosedIndex >= _videoList.count){
        return;
    }
    
    RGRecommendVideoModel *model = _videoList[chosedIndex];
    NSString *token = [RGUserInfoModel getUserInfo].token;
    NSInteger focusUserId = model.videoUser.ID;
    NSDictionary *dic = @{@"id":@(focusUserId),
                          @"token":SafeStr(token)
                          };
    
    if(!model.videoUser.meFollowmTa){//我没有关注对方
        [self.protocol showLoading:@"正在关注Ta"];
        AA_WEAK_MAKE(self) weakSelf = self;
        HttpRequest *quest = [HttpRequest buildRequest:@"/api/userFans/add"];
        [quest post:dic success:^(id data) {
            [weakSelf.protocol dismissLoading];
            [weakSelf.protocol showTextMessageWithMessage:@"成功关注Ta"];
            model.videoUser.meFollowmTa = YES;
            if([weakSelf.protocol respondsToSelector:@selector(changeFocusStatusWithIsFocus:currentCellIndex:)]){
                [weakSelf.protocol changeFocusStatusWithIsFocus:YES currentCellIndex:chosedIndex];
            }
            [weakSelf updateVedioUsersFocusStatusWithIsFocus:YES vedioUserId:model.videoUser.ID];
        } fail:^(NSInteger code, NSString *message) {
            
            [weakSelf.protocol showTextMessageWithMessage:message];
        }];
    }else{//我关注了对方
        [self.protocol showLoading:@"正在取消关注Ta"];
        AA_WEAK_MAKE(self) weakSelf = self;
        HttpRequest *quest = [HttpRequest buildRequest:@"/api/userFans/cancel"];
        [quest post:dic success:^(id data) {
            [weakSelf.protocol dismissLoading];
            [weakSelf.protocol showTextMessageWithMessage:@"成功取消关注Ta"];
            model.videoUser.meFollowmTa = NO;
            if([weakSelf.protocol respondsToSelector:@selector(changeFocusStatusWithIsFocus:currentCellIndex:)]){
                [weakSelf.protocol changeFocusStatusWithIsFocus:NO currentCellIndex:chosedIndex];
            }
            [weakSelf updateVedioUsersFocusStatusWithIsFocus:NO vedioUserId:model.videoUser.ID];
        } fail:^(NSInteger code, NSString *message) {
            
            [weakSelf.protocol showTextMessageWithMessage:message];
        }];
    }
}

-(void) updateVedioUsersFocusStatusWithIsFocus:(BOOL)isFocus vedioUserId:(NSInteger)userId{
    for (RGRecommendVideoModel *model in _videoList) {
        if(model.videoUser.ID == userId){
            model.videoUser.meFollowmTa = isFocus;
            NSLog(@"id= %ld",userId);
        }
    }
}

//点赞
-(void) changeFavoriteStautswithChosedIndex:(NSInteger)chosedIndex{
    if([_videoList count] == 0 || chosedIndex >= _videoList.count){
        return;
    }
    RGRecommendVideoModel *model = _videoList[chosedIndex];
    NSString *token = [RGUserInfoModel getUserInfo].token;
    NSInteger vedioId = model.ID;
    NSDictionary *dic = @{@"id":@(vedioId),
                          @"token":SafeStr(token)
                          };
    if(model.isLike){//当前状态是已点赞
        [self.protocol showLoading:@"正在取消点赞"];
        AA_WEAK_MAKE(self) weakSelf = self;
        HttpRequest *quest = [HttpRequest buildRequest:@"/api/userLike/cancel"];
        [quest post:dic success:^(id data) {
            [weakSelf.protocol dismissLoading];
            [weakSelf.protocol showTextMessageWithMessage:@"成功取消点赞"];
            model.isLike = NO;
            model.likeCounts = ((model.likeCounts - 1) >= 0)? (model.likeCounts - 1):0;
            if([weakSelf.protocol respondsToSelector:@selector(changeFavoriteStatusWithIsFavorite:currentCellIndex:)]){
                [weakSelf.protocol changeFavoriteStatusWithIsFavorite:NO currentCellIndex:chosedIndex];
            }
//            [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationLoversArrayChanged object:model];
        } fail:^(NSInteger code, NSString *message) {
            
            [weakSelf.protocol showTextMessageWithMessage:message];
        }];
    }else{
        [self.protocol showLoading:@"正在点赞"];
        AA_WEAK_MAKE(self) weakSelf = self;
        HttpRequest *quest = [HttpRequest buildRequest:@"/api/userLike/add"];
        [quest post:dic success:^(id data) {
            [weakSelf.protocol dismissLoading];
            [weakSelf.protocol showTextMessageWithMessage:@"成功点赞"];
            model.isLike = YES;
             model.likeCounts = model.likeCounts + 1;
            if([weakSelf.protocol respondsToSelector:@selector(changeFavoriteStatusWithIsFavorite:currentCellIndex:)]){
                [weakSelf.protocol changeFavoriteStatusWithIsFavorite:YES currentCellIndex:chosedIndex];
            }
//            [[NSNotificationCenter defaultCenter] postNotificationName:KNotificationLoversArrayChanged object:model];
        } fail:^(NSInteger code, NSString *message) {
            
            [weakSelf.protocol showTextMessageWithMessage:message];
        }];
    }
}


//第一次获取评论列表
-(void) gainVideoRecommentListWithChosedIndex:(NSInteger) chosedIndex isGainMore:(BOOL)isGainMore isShowLoading:(BOOL)isShowLoading{
    
    if([_videoList count] == 0 || chosedIndex >= _videoList.count){
        return;
    }
    _currentVedioIndex = chosedIndex;

    if(isGainMore){
        _mainCommentPageNum ++;
    }else{
        _mainCommentPageNum = 1;
        _mainCommentList = [NSMutableArray array];
    }
    
    RGRecommendVideoModel *model = _videoList[chosedIndex];
//    NSString *token = [RGUserInfoModel getUserInfo].token;
    NSInteger vedioId = model.ID;
     _currentVedioId = vedioId;
    NSDictionary *dic = @{@"id":@(vedioId),
                          @"pageSize":@(pageSize),
                          @"pageNumber":@(_mainCommentPageNum)
                          };
    if(isShowLoading){
        [self.protocol showLoading:@"正在获取评论"];
    }
    AA_WEAK_MAKE(self) weakSelf = self;
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/videoComments"];
    [quest post:dic success:^(id data) {
        NSLog(@"222");
        //取消mjfresh
        if(isShowLoading){
            [weakSelf.protocol dismissLoading];
        }
        
        NSArray *originalArr = [data objectForKey:@"list"];
        NSError *error;
        if(originalArr.count){
            NSMutableArray *tempArr = [RGRecommentListModel arrayOfModelsFromDictionaries:originalArr error:&error];
            if(tempArr.count){
                if(isGainMore){//获取更多
                    if([weakSelf.mainCommentList count] == 0){
                        weakSelf.mainCommentList = tempArr;
                    }else{
                        [weakSelf.mainCommentList addObjectsFromArray:tempArr];
                    }
                }else{
                    weakSelf.mainCommentList = tempArr;
                }
            }
        }
        if([weakSelf.protocol respondsToSelector:@selector(reloadCommentTableViewWithCommentList:)]){
            [weakSelf.protocol reloadCommentTableViewWithCommentList:weakSelf.mainCommentList];
        }
        NSInteger total = [[data objectForKey:@"total"] integerValue];
        
        if(originalArr.count< pageSize){
            if([weakSelf.protocol respondsToSelector:@selector(changeCommentMjFooterStatusWithNoMoreData)]){
                [weakSelf.protocol changeCommentMjFooterStatusWithNoMoreData];
            }
        }
        
          [self updateCommentCountsWhenGainCommentListsSuccess];
        
    } fail:^(NSInteger code, NSString *message) {
        //取消mjfresh
        if(isShowLoading){
            [weakSelf.protocol dismissLoading];
        }
        [weakSelf.protocol showTextMessageWithMessage:message];
    }];
}

//刷新评论列表
-(void) gainVideoRecommentListWithIsGainMore:(BOOL)isGainMore{
    
    if([_videoList count] == 0 || _currentVedioIndex >= _videoList.count){
        return;
    }
    
    if(isGainMore){
        _mainCommentPageNum ++;
    }else{
        _mainCommentPageNum = 1;
        _mainCommentList = [NSMutableArray array];
    }
    
    RGRecommendVideoModel *model = _videoList[_currentVedioIndex];
    //    NSString *token = [RGUserInfoModel getUserInfo].token;
    NSInteger vedioId = model.ID;
    _currentVedioId = vedioId;
    NSDictionary *dic = @{@"id":@(vedioId),
                          @"pageSize":@(pageSize),
                          @"pageNumber":@(_mainCommentPageNum)
                          };
    AA_WEAK_MAKE(self) weakSelf = self;
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/videoComments"];
    [quest post:dic success:^(id data) {
        
        NSArray *originalArr = [data objectForKey:@"list"];
        NSError *error;
        if(originalArr.count){
            NSMutableArray *tempArr = [RGRecommentListModel arrayOfModelsFromDictionaries:originalArr error:&error];
            if(tempArr.count){
                if(isGainMore){//获取更多
                    if([weakSelf.mainCommentList count] == 0){
                        weakSelf.mainCommentList = tempArr;
                    }else{
                        [weakSelf.mainCommentList addObjectsFromArray:tempArr];
                    }
                }else{
                    weakSelf.mainCommentList = tempArr;
                }
            }
        }
        if([weakSelf.protocol respondsToSelector:@selector(reloadCommentTableViewWithCommentList:)]){
            [weakSelf.protocol reloadCommentTableViewWithCommentList:weakSelf.mainCommentList];
        }
        NSInteger total = [[data objectForKey:@"total"] integerValue];
        if(isGainMore){
            if([weakSelf.protocol respondsToSelector:@selector(commentMjFooterEndLoading)]){
                [weakSelf.protocol commentMjFooterEndLoading];
            }
        }else{
            if([weakSelf.protocol respondsToSelector:@selector(commentMjHeaderEndLoading)]){
                [weakSelf.protocol commentMjHeaderEndLoading];
            }
        }
      
        if(originalArr.count < pageSize){
            if([weakSelf.protocol respondsToSelector:@selector(changeCommentMjFooterStatusWithNoMoreData)]){
                [weakSelf.protocol changeCommentMjFooterStatusWithNoMoreData];
            }
        }
        
//        [self updateCommentCountsWhenGainCommentListsSuccess];
        
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.protocol showTextMessageWithMessage:message];
    }];
}

-(void) updateCommentCountsWhenGainCommentListsSuccess{
    NSInteger index = [self checkCurrentVedioModelIndex];
    if(index >= 0){
        RGRecommendVideoModel *model = _videoList[index];
        NSInteger commentCount = [self getCommentCount];
        if(model.commentsCounts != commentCount){
            model.commentsCounts = commentCount;
            if([self.protocol respondsToSelector:@selector(updateCommentCountsWhenCommentSuccessWithCellIndex:)]){
                [self.protocol updateCommentCountsWhenCommentSuccessWithCellIndex:index];
            }
        }
    }
}

-(NSInteger)getCommentCount {
    NSInteger count = _mainCommentList ? _mainCommentList.count : 0;
    if (count > 0) {
        for (RGRecommentListModel *commenModel in _mainCommentList) {
            count += commenModel.childCommentsList ? commenModel.childCommentsList.count : 0;
        }
    }
    return count;
}


//主评论点赞
-(void) changeMainCommentFavoriteStautswithChosedIndex:(NSInteger)chosedIndex{
    if([_mainCommentList count] == 0 || chosedIndex >= _mainCommentList.count){
        return;
    }
    RGRecommentListModel *model = _mainCommentList[chosedIndex];
    NSString *token = [RGUserInfoModel getUserInfo].token;
    NSInteger commentId = model.ID;
    if(model.commentsUser.ID == [RGUserInfoModel getUserInfo].ID){//是自己的评论不能点赞
        [self.protocol showAlertWhenFarvoriteSelfComment];
        return;
    }
    NSDictionary *dic = @{@"id":@(commentId),
                          @"token":SafeStr(token)
                          };
    if(model.isLike){//当前状态是已点赞
        AA_WEAK_MAKE(self) weakSelf = self;
        HttpRequest *quest = [HttpRequest buildRequest:@"/api/comments/commentsLikeCancle"];
        [quest post:dic success:^(id data) {
        
            model.isLike = NO;
            model.likeCounts = ((model.likeCounts - 1) >= 0)? (model.likeCounts - 1):0;
            //点赞刷新UI
            if([weakSelf.protocol respondsToSelector:@selector(reloadCommentTableViewSectionWithIndex:)]){
                [weakSelf.protocol reloadCommentTableViewSectionWithIndex:chosedIndex];
            }
        } fail:^(NSInteger code, NSString *message) {
          [weakSelf.protocol showTextMessageWithMessage:message];
        }];
    }else{
      
        AA_WEAK_MAKE(self) weakSelf = self;
        HttpRequest *quest = [HttpRequest buildRequest:@"/api/comments/commentsLike"];
        [quest post:dic success:^(id data) {
          
            model.isLike = YES;
            model.likeCounts = model.likeCounts + 1;
            //点赞刷新UI
            if([weakSelf.protocol respondsToSelector:@selector(reloadCommentTableViewSectionWithIndex:)]){
                [weakSelf.protocol reloadCommentTableViewSectionWithIndex:chosedIndex];
            }
        } fail:^(NSInteger code, NSString *message) {
            [weakSelf.protocol showTextMessageWithMessage:message];
        }];
    }
}

//子评论点赞
-(void) changeChildCommentFavoriteStautswithChosedIndex:(NSIndexPath*)path{
    if([_mainCommentList count] == 0 || path.section >= _mainCommentList.count){
        return;
    }
    RGRecommentListModel *model = _mainCommentList[path.section];
    
    if([model.childCommentsList count] == 0 || path.row >= [model.childCommentsList count]){
        return;
    }
    NSString *token = [RGUserInfoModel getUserInfo].token;
    RGChildRecommentListModel *childModel = model.childCommentsList[path.row];
    NSInteger childCommentId = childModel.ID;
    if(childModel.commentsUser.ID == [RGUserInfoModel getUserInfo].ID){//是自己的评论不能点赞
        [self.protocol showAlertWhenFarvoriteSelfComment];
        return;
    }
    NSDictionary *dic = @{@"id":@(childCommentId),
                          @"token":SafeStr(token)
                          };
    if(childModel.isLike){//当前状态是已点赞
        AA_WEAK_MAKE(self) weakSelf = self;
        HttpRequest *quest = [HttpRequest buildRequest:@"/api/comments/commentsLikeCancle"];
        [quest post:dic success:^(id data) {
            
            childModel.isLike = NO;
            childModel.likeCounts = ((childModel.likeCounts - 1) >= 0)? (childModel.likeCounts - 1):0;
            //点赞刷新UI
            if([weakSelf.protocol respondsToSelector:@selector(reloadCommentTableViewCellWithIndexPath:)]){
                [weakSelf.protocol reloadCommentTableViewCellWithIndexPath:path];
            }
        } fail:^(NSInteger code, NSString *message) {
            
        }];
    }else{
        
        AA_WEAK_MAKE(self) weakSelf = self;
        HttpRequest *quest = [HttpRequest buildRequest:@"/api/comments/commentsLike"];
        [quest post:dic success:^(id data) {
            
            childModel.isLike = YES;
            childModel.likeCounts = childModel.likeCounts + 1;
            //点赞刷新UI
            if([weakSelf.protocol respondsToSelector:@selector(reloadCommentTableViewCellWithIndexPath:)]){
                [weakSelf.protocol reloadCommentTableViewCellWithIndexPath:path];
            }
        } fail:^(NSInteger code, NSString *message) {
            [weakSelf.protocol showTextMessageWithMessage:message];
        }];
    }
}


//发表评论
-(void) addMainCommentWithContent:(NSString *)content{
    if(_currentVedioId <= 0){
        return;
    }
    NSString *token = [RGUserInfoModel getUserInfo].token;
    NSDictionary *dic = @{@"id":@(_currentVedioId),
                          @"token":SafeStr(token),
                          @"content":SafeStr(content),
                          @"type":@(0)
                          };
    AA_WEAK_MAKE(self) weakSelf = self;
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/comments/replyComments"];
    [quest post:dic success:^(id data) {
        NSDictionary *dic = [data objectForKey:@"comments"];
        NSError *error;
        if([dic count]){
            RGRecommentListModel *model = [[RGRecommentListModel alloc]initWithDictionary:dic error:&error];
            if(error == nil){
                if([weakSelf.mainCommentList count] == 0){
                    weakSelf.mainCommentList = [NSMutableArray array];
                    [weakSelf.mainCommentList addObject:model];
                }else{
                    [weakSelf.mainCommentList insertObject:model atIndex:0];
                }
            }
        }
        if([weakSelf.protocol respondsToSelector:@selector(reloadCommentTableViewWithCommentList:)]){
            [weakSelf.protocol reloadCommentTableViewWithCommentList:weakSelf.mainCommentList];
        }
         if([weakSelf.protocol respondsToSelector:@selector(resetCommentStatusWhenPublicSuccess)]){
             [weakSelf.protocol resetCommentStatusWhenPublicSuccess];
         }
        [self updateVedioCommentCountWhenpublishCommentSuccess];
      
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.protocol showTextMessageWithMessage:message];
    }];
}

//主评论发表成功后,刷新对应视频的评论的评论数
-(void) updateVedioCommentCountWhenpublishCommentSuccess{
      NSInteger index = [self checkCurrentVedioModelIndex];
    if(index >= 0){
        RGRecommendVideoModel *model = _videoList[index];
        model.commentsCounts = [self getCommentCount];
        if([self.protocol respondsToSelector:@selector(updateCommentCountsWhenCommentSuccessWithCellIndex:)]){
            [self.protocol updateCommentCountsWhenCommentSuccessWithCellIndex:index];
        }
    }
}

//找到视频model
-(NSInteger)checkCurrentVedioModelIndex{
    NSInteger result = -1;
    for (NSInteger index = 0; index < _videoList.count; index ++) {
         RGRecommendVideoModel *tempModel = _videoList[index];
        if(tempModel.ID == _currentVedioId){
            result = index;
            break;
        }
    }
    return result;
}

//发表子评论
-(void) addChildCommentWithContent:(NSString *)content chosedIndexPath:(NSIndexPath *)path chosedIndex:(NSInteger)chosedIndex{
    if(_currentVedioId <= 0){
        return;
    }
    RGRecommentListModel *model;
    NSInteger commentId;
    NSInteger commentUserId;
    if(path == nil){//点击主评论发表子评论
        model = _mainCommentList[chosedIndex];
        if(model == nil){
            return;
        }
        commentUserId = model.commentsUser.ID;
        commentId = model.ID;
    }else{
        model = _mainCommentList[path.section];
        RGRecommentListModel *childModel = model.childCommentsList[path.row];
        if(childModel == nil){
            return;
        }
        commentUserId = childModel.commentsUser.ID;
        commentId = childModel.ID;
    }
    
    NSString *token = [RGUserInfoModel getUserInfo].token;
    RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
    if(user.ID == commentUserId){
        [self.protocol showAlertWhenCommentSelfComment];
        return;
    }
    
    NSDictionary *dic = @{@"id":@(_currentVedioId),
                          @"token":SafeStr(token),
                          @"content":SafeStr(content),
                          @"type":@(0),
                          @"commentsId":@(commentId)
                          };
    AA_WEAK_MAKE(self) weakSelf = self;
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/comments/replyComments"];
    [quest post:dic success:^(id data) {
        NSDictionary *dic = [data objectForKey:@"comments"];
        NSError *error;
        if([dic count]){
            RGChildRecommentListModel *childModel = [[RGChildRecommentListModel alloc]initWithDictionary:dic error:&error];
            if(error == nil){
                if([model.childCommentsList count] == 0){
                    model.childCommentsList = [@[childModel] mutableCopy];
                }else{
                    [model.childCommentsList insertObject:childModel atIndex:0];
                }
            }
        }
        if([weakSelf.protocol respondsToSelector:@selector(reloadCommentTableViewWithCommentList:)]){
            [weakSelf.protocol reloadCommentTableViewWithCommentList:weakSelf.mainCommentList];
        }
        if([weakSelf.protocol respondsToSelector:@selector(reloadCommentTableViewSectionWithIndex:)]){
            [weakSelf.protocol reloadCommentTableViewSectionWithIndex:path.section];
        }
        if([weakSelf.protocol respondsToSelector:@selector(resetCommentStatusWhenPublicSuccess)]){
            [weakSelf.protocol resetCommentStatusWhenPublicSuccess];
        }
        [self updateVedioCommentCountWhenpublishCommentSuccess];
        
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.protocol showTextMessageWithMessage:message];
    }];
}

//转发视频
-(void) shareVideoWithContent:(NSString *)content chosedIndex:(NSInteger)index{
    if(index >= [_videoList count] || index < 0){
        return;
    }
    RGRecommendVideoModel *vedioModel = _videoList[index];
    NSString *token = [RGUserInfoModel getUserInfo].token;
    NSDictionary *dic = @{@"id":@(vedioModel.ID),
                          @"token":SafeStr(token),
                          @"content":SafeStr(content)
                          };
    AA_WEAK_MAKE(self) weakSelf = self;
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/videos/videosForward"];
    [self.protocol showLoading:@""];
    [quest post:dic success:^(id data) {
        [weakSelf.protocol dismissLoading];
        [weakSelf.protocol showTextMessageWithMessage:@"转发成功"];
        if([weakSelf.protocol respondsToSelector:@selector(dismissToolBarView)]){
            [weakSelf.protocol dismissToolBarView];
        }
        [weakSelf updateShareVedioCountWhenShareSuccessWithchosedIndex:index];
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf.protocol dismissLoading];
        [weakSelf.protocol showTextMessageWithMessage:message];
    }];
}

//转发成功后更新vedio的转发次数
-(void) updateShareVedioCountWhenShareSuccessWithchosedIndex:(NSInteger)index{
    if(index >= [_videoList count] || index < 0){
        return;
    }
   RGRecommendVideoModel *model = _videoList[index];
   model.forwardCounts = model.forwardCounts + 1;
    if([self.protocol respondsToSelector:@selector(updateforwardCountsWhenShareSuccessWithCellIndex:)]){
        [self.protocol updateforwardCountsWhenShareSuccessWithCellIndex:index];
    }
}


//跳转到聊天界面
-(void) pushToChatVcWithChosedVedioIndex:(NSInteger)index{
    if([_videoList count] == 0 || index >= _videoList.count){
        return;
    }
    RGRecommendVideoModel *model = _videoList[index];
    NSInteger userId = [RGUserInfoModel getUserInfo].ID;
    if(userId == model.videoUser.ID){
        [self.protocol showTextMessageWithMessage:@"不能和自己发私信"];
    }else{
       [RGMainPageRouter pushToLoginWithPassViewControllerFromVC:(UIViewController *)self.protocol userId:userId toUserId:model.videoUser.ID nickName:model.videoUser.nickname];
    }
}

//跳转到个人信息页面
-(void) pushToPersonalVcWithChosdeVedioIndex:(NSInteger)index{
    if([_videoList count] == 0 || index >= _videoList.count){
        return;
    }
    AA_WEAK_MAKE(self)weakSelf = self;
    void (^FoucsChangedBlock)(BOOL) = ^(BOOL isFoucs){
        RGRecommendVideoModel *model = weakSelf.videoList[index];
        model.videoUser.meFollowmTa = isFoucs;
        if([weakSelf.protocol respondsToSelector:@selector(changeFocusStatusWithIsFocus:currentCellIndex:)]){
            [weakSelf.protocol changeFocusStatusWithIsFocus:isFoucs currentCellIndex:index];
        }
    };
    RGRecommendVideoModel *model = _videoList[index];
    [RGMainPageRouter pushToPersonInfoViewControllerFromVC:(UIViewController *)self.protocol userId:model.videoUser.ID meFollowTa:model.videoUser.meFollowmTa nickName:model.videoUser.nickname focusChangedBlock:FoucsChangedBlock];
}

//扫一扫
-(void)showScanViewController{
    [RGMainPageRouter pushToQRcodeScanViewControllerFromVC:(UIViewController *)self.protocol];
}

//搜索
-(void) showSearchViewController{
    [RGMainPageRouter pushToRGSearchViewControllerFromVC:(UIViewController *)self.protocol];
}

//分享
-(void) shareToWeChatWithChosedVedioIndex:(NSInteger)index{
    if([_videoList count] == 0 || index >= _videoList.count){
        return;
    }
     RGRecommendVideoModel *model = self.videoList[index];
    [XLMShareSdkTool shareVedioWithPlatformType:SSDKPlatformTypeWechat Text:model.videoDesc title:@"红草视界" url:model.videoPath thumbImage:IMAGE_NAMED(@"icon1") image:nil musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil sourceFileExtension:nil sourceFileData:nil type:SSDKContentTypeVideo onStateChanged:^(SSDKResponseState state) {
        
    }];
}

//下载视频
-(void) downLoadVedioWithChosedVedioIndex:(NSInteger)index{
    if([_videoList count] == 0 || index >= _videoList.count){
        return;
    }
    RGRecommendVideoModel *model = self.videoList[index];
    [[RGRemoteVedioDownLoadHelper downLoadhepler] downLoadRemoteVedioWithUrl:model.videoPath];
}

//更新视频播放量
-(void) updateVeidoPlayCountsWithChosedVedioIndex:(NSInteger)index{
    if([_videoList count] == 0 || index >= _videoList.count){
        return;
    }
    RGRecommendVideoModel *model = self.videoList[index];
    AA_WEAK_MAKE(self)weakSelf = self;
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/playCount"];
    [quest post:@{@"id":@(model.ID)} success:^(id data) {
        NSLog(@"更新成功");
        RGRecommendVideoModel *model = weakSelf.videoList[index];
        model.playCount = model.playCount + 1;
    } fail:^(NSInteger code, NSString *message) {
        
    }];
}

-(void)changeTabbarUiStyleWithChosedVedioIndex:(NSInteger)index{
//    if(index < 0){
//         return;
//    }
//    if(index > self.videoList.count){
//        return;
//    }
//    RGRecommendVideoModel *model = self.videoList[index];
//    if(model.width == 0){
//        [[AppDelegate shareAppDelegate] changeTabbarUiStyleWithIsClear:YES];
//    }else{
//        if(KScreenHeight * 9  > KScreenWidth * 16){
//            [[AppDelegate shareAppDelegate] changeTabbarUiStyleWithIsClear:NO];
//        }else{
//            [[AppDelegate shareAppDelegate] changeTabbarUiStyleWithIsClear:YES];
//        }
//    }
}

@end
